#include "mainwindow.h"
#include "ui_mainwindow.h"

#ifdef _WIN32
      int d_system_type = WINDOWS;
#else
#include <unistd.h>
#include <linux/reboot.h>
#include <sys/reboot.h>
      int d_system_type = Linux;
#endif
//static QString widgetbackgroundSheet ="background-color:  qconicalgradient(cx:0.5, cy:0, angle:0, stop:0 rgba(55, 55, 202, 100), stop:1 rgba(207, 179, 179, 255))";
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setsystemshow();
    qRegisterMetaType<atime_t>("atime_t");//注册PortConfig类型
    qRegisterMetaType<c_type>("c_type");//注册PortConfig类型
    memset(&mshut,0,sizeof(mshut));
    ui->widget_note->hide();
    time_worker = new timethread;
    mythread=new QThread(this);//Create child thread
    time_worker->moveToThread(mythread);//Add custom threads to child threads

    connect(mythread,&QThread::finished,time_worker,&QObject::deleteLater);  //Thread suicide through deleteLater
    connect(time_worker,SIGNAL(time_Signal(atime_t)),this,SLOT(Show_time(atime_t)));//Processing real-time clock signals sent by sub-threads
    connect(this, SIGNAL(kill_Signal(c_type)), time_worker, SLOT(shotdown(c_type)));
    connect(this, &MainWindow::time_Start, time_worker, &timethread::beginToWork); //Connect signal to thread start function
    mythread->start();//Start the thread, but have not started the thread processing function
    emit time_Start();

    if(d_system_type == Linux)
    {
      ui->widget_screen->hide();
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::setsystemshow()
{
 switch(d_system_type)
 {
  case WINDOWS:
  {
    ui->label_cursystem->setText("Windows");
  }
    break;
  case Linux:
    ui->label_cursystem->setText("Linux");
    break;
  default:
    ui->label_cursystem->setText("");
     break;
 }


}
//用于接收子线程发来的时间数据
void MainWindow::Show_time(atime_t mtime)
{
    ui->label_systemtime->setText(mtime.systime.toString("MM/dd hh:mm:ss"));
    if(work==true)
       ui->label_Countdown->setText(mtime.Countdowntime.toString("hh:mm:ss"));
}

void MainWindow::on_radioButton_1_clicked()
{
 if(ui->radioButton_1->isChecked())
 {
  ui->radioButton_2->setChecked(false);
 }
 else
    ui->radioButton_2->setChecked(true);
}

void MainWindow::on_radioButton_2_clicked()
{
    if(ui->radioButton_2->isChecked())
    {
     ui->radioButton_1->setChecked(false);
    }
    else
       ui->radioButton_1->setChecked(true);
}

void MainWindow::on_pushButton_clicked()
{
      work=true;
  if(ui->radioButton_1->isChecked())
  {
      qDebug()<<QString("将在%1关机").arg(ui->timeEditpowertime_1->time().toString("hh:mm"))<<endl;
      mshut.endtime=ui->timeEditpowertime_1->time();
      mshut.type=1;
      emit kill_Signal(mshut);
  }
  else if(ui->radioButton_2->isChecked())
  {
     qDebug()<<QString("将在%1后关机").arg(ui->timeEditpowertime_2->time().toString("hh:mm"))<<endl;
     mshut.endtime=ui->timeEditpowertime_2->time();
     mshut.type=2;
     emit kill_Signal(mshut);
  }

  if(ui->checkBox->isChecked() && d_system_type != Linux)
  {
      mshut.endtime=ui->timeEdit_screen->time();
      mshut.type=3;
      emit kill_Signal(mshut);
  }
  ui->radioButton_1->setEnabled(false);
  ui->radioButton_2->setEnabled(false);
  ui->timeEditpowertime_1->setEnabled(false);
  ui->timeEditpowertime_2->setEnabled(false);
  ui->widget_set->setStyleSheet("background-color: rgb(204, 204, 204);");
  ui->widget_note->show();
}

void MainWindow::on_pushButton_2_clicked()
{
  work=false;
  ui->radioButton_1->setEnabled(true);
  ui->radioButton_2->setEnabled(true);
  ui->timeEditpowertime_1->setEnabled(true);
  ui->timeEditpowertime_2->setEnabled(true);
  ui->timeEditpowertime_1->setTime(QTime(0,0,0));
  ui->timeEditpowertime_2->setTime(QTime(0,0,0));
  ui->widget_set->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 178, 102, 255), stop:0.55 rgba(235, 148, 61, 255), stop:0.98 rgba(0, 0, 0, 255), stop:1 rgba(0, 0, 0, 0));");
  ui->widget_note->hide();
  memset(&mshut,0,sizeof(mshut));
  emit kill_Signal(mshut);
}
